package com.xdxc.service.strategy.gradingstrategy;


import com.xdxc.annotation.GradingStrategyType;
import com.xdxc.emuns.QuestionType;
import com.xdxc.model.enums.AutoGradeQuestionType;
import com.xdxc.model.dto.CorrectAnswerDTO;
import com.xdxc.model.entity.GradingResult;
import com.xdxc.model.entity.TempUserAnswer;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;


@Component
@GradingStrategyType(AutoGradeQuestionType.SINGLE_CHOICE)
public class SingleChoiceGradingStrategy implements GradingStrategy {
    @Override
    public GradingResult grade(TempUserAnswer tempUserAnswer, CorrectAnswerDTO correctAnswerDTO) {
//        // 1. 验证答案格式
        Set<Long> selectedIds = new HashSet<>(tempUserAnswer.getAnswerIds());  // 直接收集为 Long
//        // 2. 验证选项有效性
////         3. 获取正确答案
        Set<String> correctAnswerSet =  Optional.ofNullable(correctAnswerDTO.getOptionIds())
                .orElse(Collections.emptyList())
                .stream()
                .map(String::valueOf)  // 确保所有 ID 是 String 类型
                .collect(Collectors.toSet());

        boolean isCorrect = correctAnswerSet.equals(selectedIds);

        BigDecimal score = isCorrect ? BigDecimal.valueOf(correctAnswerDTO.getScore()) : BigDecimal.ZERO;
        GradingResult gradingResult1 = new GradingResult();
        gradingResult1.setAnswerId(tempUserAnswer.getId());
        gradingResult1.setIsCorrect(isCorrect);
        gradingResult1.setQuestionType(QuestionType.getCodeByDesc(String.valueOf(tempUserAnswer.getAnswerType())));
        gradingResult1.setUserScore(score);
        gradingResult1.setPaperId(tempUserAnswer.getPaperId());
        // 4. 构建结果
        return gradingResult1;
    }




}